import deepCopy from 'src/hrpub/common/utils/deep-copy';
import {addTempleteQueCon, COMMON, setFormDisabled} from "../../../../../main/util";

export default class FormAction {
    constructor(comp) {
        this.comp = comp;
        this.dispatch = this.comp.props.dispatch;
    }

    /**
     * 表单的编辑前事件
     * @param props
     * @param moduleId
     * @param key
     * @param value
     * @param data
     * @returns {Promise<boolean>}
     */
    onBeforeEvent = async (props, moduleId, key, value, data) => {
        let template = props.meta.getMeta();
        let formArr = deepCopy(template.formrelation[COMMON.formId2]);
        formArr.push(COMMON.formId2);
        let isRefer = false;
        formArr.map(formId => {
            template[formId].items.map((item) => {
                if (item.attrcode === key) {
                    isRefer = !item.disabled && (item.itemtype === 'refer');
                }
            });
        });
        if (!isRefer) return true;
        let postData = {
            key,
            pk_org: data.pk_org.value,
            tansMode: data.stapply_mode.value,
            formData: props.form.getAllFormValue(COMMON.formId2)
        };
        try {
            let res = await this.dispatch({
                type: 'transfer/beforeEditAction',
                payload: {
                    postData
                }
            });
            if (!res.success) return;
            if (res.data && res.data.refParam) {

                formArr.map(formId => {
                    template[formId].items.map((item) => {
                        if (item.attrcode === key) {
                            if (!item.queryCondition) item.queryCondition = {};
                            Object.keys(item.queryCondition).forEach(keyW=>{
                                delete item.queryCondition[keyW]
                            });
                            Object.assign(item.queryCondition, res.data.refParam);
                        }
                    });
                })
            }
            await new Promise((resolve) => {
                addTempleteQueCon(template);
                props.meta.setMeta(template, resolve);
            });
        } catch (e) {

        }
        return true
    };

    /**
     * 表单的编辑后事件
     * @param props
     * @param moduleId
     * @param key
     * @param value
     * @param oldValue
     * @returns {Promise<void>}
     */
    onAfterEvent = async (props, moduleId, key, value, oldValue) => {
        let form = props.form;
        let formData = form.getAllFormValue(COMMON.formId2);
        let values = formData.rows[0].values;
        if (moduleId === 'card_batch' && key === 'transtypeid') {
            let v = value.values? value.values['pk_billtypecode'] : {}
            await this.dispatch({
                type: 'transfer/update',
                payload: {
                    transtypecode: v
                }
            });
        } 
        
        let postData = {
            app_code: COMMON.appConfig.appcode,
            page_code: '60092050batch',
            area_code: COMMON.formId2,
            formData,
            key,
            pk_org: values.pk_org.value,
        };
        try {
            let res = await this.dispatch({
                type: 'transfer/afterEditAction',
                payload: {
                    postData
                }
            });
            if (!res.success) return;
            if (res && res.data && res.data.errorMsg) {
                toast({
                    color: 'danger',
                    content: res.data.errorMsg
                });
            }
            //设置form
            let {formData, visible, disable, required} = res.data;
            props.form.setFormItemsVisible(moduleId, visible);
            props.form.setFormItemsDisabled(moduleId, disable);
            props.form.setFormItemsRequired(moduleId, required);

            props.form.setAllFormValue({
                [COMMON.formId2]: {
                    rows: formData[COMMON.formId2].rows
                }
            });
        } catch (e) {

        }
    };

}
